﻿using NFine.Code.Excel;
using NFine.Data.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NBProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = this.openFileDialog1.FileName;

                byte[] bBuffer;

                FileStream fs = new FileStream(this.textBox1.Text, FileMode.Open);
                BinaryReader binReader = new BinaryReader(fs);

                bBuffer = new byte[fs.Length];
                binReader.Read(bBuffer, 0, (int)fs.Length);



                PointsDataStruct info = new PointsDataStruct();
                int sl = Marshal.SizeOf(info);
                if (bBuffer.Length % sl != 0)
                {
                    MessageBox.Show("导入文件数据结构错误，不是数据结构的整数倍");
                    return;
                }
                int num = bBuffer.Length / sl;

                List<PointsDataStruct> list = new List<PointsDataStruct>();
                for (int i = 0; i < num; i++)
                {
                    //分配结构体内存空间
                    IntPtr structPtr = Marshal.AllocHGlobal(Marshal.SizeOf(info));
                    Marshal.Copy(bBuffer, sl * i, structPtr, sl);
                    PointsDataStruct points = (PointsDataStruct)Marshal.PtrToStructure(structPtr, typeof(PointsDataStruct));
                    list.Add(points);
                    //释放内存空间
                    Marshal.FreeHGlobal(structPtr);
                }            
                binReader.Close();
                fs.Close();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {



            DataTable dt = NPOIExcel.ExcelToDataTable(this.textBox1.Text);
            int ret = Export.ExportSlope(dt);
            if (ret == -2)
            {
                MessageBox.Show("导入失败，请检测文件格式是否正确！");
            }
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
        public struct PointsDataStruct
        {

            int KM;
            int Meter;
            int m_fChuiMoL;
            int m_fCeMoL;
            int m_fLxLA;
            int m_fLyLA;
            int m_fSxLA;
            int m_fSyLA;
            int m_fFenjiedianLA;
            int m_fLxLB;
            int m_fLyLB;
            int m_fSxLB;
            int m_fSyLB;
            int m_fFenjiedianLB;
            int m_fChuiMoR;
            int m_fCeMoR;
            int m_fLxRA;
            int m_fLyRA;
            int m_fSxRA;
            int m_fSyRA;
            int m_fFenjiedianRA;
            int m_fLxRB;
            int m_fLyRB;
            int m_fSxRB;
            int m_fSyRB;
            int m_fFenjiedianRB;
            int m_fgage;
            int m_ftopL;
            int m_ftopR;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawXLA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawYLA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawXLB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawYLB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageXLA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageYLA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageXLB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageYLB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCXLA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCYLA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCXLB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCYLB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawXRA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawYRA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawXRB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsDrawYRB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageXRA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageYRA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageXRB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 19, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsGageYRB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCXRA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCYRA;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCXRB;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20, ArraySubType = UnmanagedType.SysInt)]
            int[] m_fPointsCYRB;
            char chrNewline1;
            char chrNewline2;
        };

    }
}
